﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using AplicacionMVC.Models;

namespace AplicacionMVC.Controllers
{
    public class OrdenServicioController : Controller
    {

        private string ordenRESTService = "http://localhost:1518/Ordenes.svc/Ordenes";
        JavaScriptSerializer js = new JavaScriptSerializer();
          
        //
        // GET: /OrdenServicio/

        public ActionResult Index()
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ordenRESTService);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string ordenesJson = reader.ReadToEnd();
            List<OrdenServicio> ordenes = js.Deserialize<List<OrdenServicio>>(ordenesJson);
            return View(ordenes);
                        
        }

        //
        // GET: /OrdenServicio/Details/5

        public ActionResult Details(String id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ordenRESTService + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string ordenesJson = reader.ReadToEnd();
            OrdenServicio ordenes = js.Deserialize<OrdenServicio>(ordenesJson);
            return View(ordenes);

         }

        //
        // GET: /OrdenServicio/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /OrdenServicio/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
             
                string postdata = "{\"Codigo\":\"" + collection["Codigo"] + "\",\"Fecha\":\"" + collection["Fecha"] + "\",\"Obs\":\"" + collection["Obs"] + "\",\"Placa\":\"" + collection["Codigo"] + "\"}"; //JSON
                  
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ordenRESTService);
                req.Method = "POST";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                //return RedirectToAction("Index");
                //return RedirectToAction("Index");
                
                return View();
            }
            catch (Exception e)
            {
                ModelState.AddModelError("Error", e.Message);
                return View();
            }
        }
              
        
        //
        // GET: /OrdenServicio/Edit/5
 
        public ActionResult Edit(int id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ordenRESTService + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string ordenJson = reader.ReadToEnd();
            OrdenServicio ordenes = js.Deserialize<OrdenServicio>(ordenJson);
            return View(ordenes);
        }

        //
        // POST: /OrdenServicio/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                //string postdata = "{\"Codigo\":\"" + id + "\",\"Nombre\":\"" + collection["Nombre"] + "\"}";
                string postdata = "{\"Codigo\":\"" + collection["Codigo"] + "\",\"Fecha\"" + collection["Fecha"] + "\",\"Obs\"" + collection["Obs"] + "\",\"Placa\":\"" + collection["Codigo"] + "\",\"}"; //JSON
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ordenRESTService);
                req.Method = "PUT";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /OrdenServicio/Delete/5
 
        public ActionResult Delete(int id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ordenRESTService + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string ordenJson = reader.ReadToEnd();
            OrdenServicio ordenes = js.Deserialize<OrdenServicio>(ordenJson);
            return View(ordenes);
        }

        //
        // POST: /OrdenServicio/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                string postdata = "\"" + id + "\"";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ordenRESTService);
                req.Method = "DELETE";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
